package com.yuanye.gwcit.fragments;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.yuanye.gwcit.R;
import com.yuanye.yeecho.base.BaseFragment;

public class HdmiOutFragment extends BaseFragment {

    private String TAG = HdmiOutFragment.class.getSimpleName();
    private HdmiReceiver hdmiReceiver;
    private MediaPlayer mediaPlayer;
    private AlertDialog dialog;

    public HdmiOutFragment() {
        setTaskName("HDMI输出");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        hideTestButton();
        View view = inflater.inflate(R.layout.frag_hdmiout, container, false);
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.intent.action.HDMI_PLUGGED");
        filter.addAction("android.intent.action.HDMISTATUS_CHANGED");
        if (hdmiReceiver == null){
            hdmiReceiver = new HdmiReceiver();
            getContext().registerReceiver(hdmiReceiver, filter);
        }
        dialog = new AlertDialog.Builder(getContext())
                .setCancelable(true)
                .setMessage("HDMI OUT测试是否通过？")
                .setPositiveButton("通过", (dialog, which) -> {
                    passAndNext();
                })
                .setNegativeButton("失败", (dialog, which) -> {
                    setFail();
                }).create();
        return view;
    }

    @Override
    public void execTask() {
    }

    @Override
    public void stopTask() {
        stopMedia();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (hdmiReceiver != null){
            getContext().unregisterReceiver(hdmiReceiver);
        }
    }

    private void playMedia() {
        if(mediaPlayer != null){
            mediaPlayer.release();
            mediaPlayer = null;
        }
        mediaPlayer = MediaPlayer.create(getContext(), R.raw.qzgy);
        mediaPlayer.setVolume(0.15f, 0.15f);
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.start();
            }
        });
        mediaPlayer.start();
    }

    private void stopMedia(){
        if(mediaPlayer != null){
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    class HdmiReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("android.intent.action.HDMISTATUS_CHANGED") || action.equals("android.intent.action.HDMI_PLUGGED")) {
                boolean state = intent.getBooleanExtra("state", false);
                Log.d(TAG, "state: "+ state);
                Log.d(TAG, "action: "+ action);
                //只在页面显示时才有动作
                if (!isHidden()){
                    if (state) {
                        // hdmi插入
                        setStatusMsg("检测到hdmi out");
                        playMedia();
                        dialog.show();

                    } else {
                        // hdmi拔出
                        setStatusMsg("未检测到有hdmi输出");
                        stopMedia();
                        dialog.dismiss();
//                    Toast.makeText(context, "hdmi拔出", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        }
    }
}
